#include "threatsoperationthread.h"
#include "invokers/invokerfactory.h"

#include <QDebug>
#include <QFileInfo>
#include <QTimer>

ThreatsOPerationThread::ThreatsOPerationThread(QObject *parent)
    : QObject(parent)
    , m_DataInterFaceServer(nullptr)
{
    // 安全中心联网管控dbus接口
    m_DataInterFaceServer = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.datainterface",
                                                                        "/com/deepin/defender/datainterface",
                                                                        "com.deepin.defender.datainterface",
                                                                        ConnectType::SESSION, this);
}

// 获得未处理异常的文件列表
void ThreatsOPerationThread::getThreatFileList()
{
    QString sendpath;
    // 1.得到所有的没有处理的异常文件
    // 得到表中所有的数据
    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_DataInterFaceServer, "notifySelectThreatCount");
    GET_MESSAGE_VALUE(QString, pathlist, msg);
    QStringList threatslist = pathlist.split("[*]");
    int threatsSize = threatslist.size() - 1;
    // 2.判断异常文件是否存在 存在保存 不存在删除
    for (int i = 0; i < threatsSize; i++) {
        QString fullFileName = threatslist.at(i);
        QFileInfo fileInfo(fullFileName);
        if (fileInfo.isFile()) {
            //如果是文件就拼接
            sendpath = sendpath + fullFileName + "*";
        } else {
            //不存在就删除
            QDBusMessage msg = DBUS_BLOCK_INVOKE(m_DataInterFaceServer, "notifyDeleteThreatDataByPath", fullFileName);
            GET_MESSAGE_VALUE(bool, bFlag, msg);
            if (!bFlag) {
                qDebug() << "删除失败=====" << fullFileName;
            }
        }
    }
    // 3.调用查杀接口
    if (!sendpath.isEmpty()) {
        emit sendThreatsFileList(sendpath);
    }
}

void ThreatsOPerationThread::addThreatsFile(QString path)
{
    // 得到表中所有的数据
    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_DataInterFaceServer, "notifySelectThreatCount");
    GET_MESSAGE_VALUE(QString, pathlist, msg);
    // 查询异常
    QStringList threatslist = pathlist.split("[*]");
    int threatsSize = threatslist.size() - 1;
    // 准备添加的异常
    QStringList readylist = path.split("[*]");
    int readySize = readylist.size() - 1;
    // 判断查询的数据中是不是有包含了准备添加的异常数据
    if (threatsSize == 0) {
        // 如果表中没有 就直接添加
        for (int j = 0; j < readySize; j++) {
            DBUS_NOBLOCK_INVOKE(m_DataInterFaceServer, "notifyAppendThreatDataByPath", readylist.at(j));
        }
    } else {
        // 否则先判断查询出来表中的数据是不是有这个文件 没有就添加
        for (int i = 0; i < readySize; i++) {
            for (int j = 0; j < threatsSize; j++) {
                if (readylist.at(i) == threatslist.at(j)) {
                    // 如果都有就移除
                    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_DataInterFaceServer, "notifyDeleteThreatDataByPath", readylist.at(i));
                    GET_MESSAGE_VALUE(bool, bFlag, msg);
                    if (!bFlag) {
                        qDebug() << "删除失败=======" << readylist.at(i);
                    }
                }
            }
        }
        // 然后准备添加的异常数据添加
        for (int j = 0; j < readySize; j++) {
            DBUS_NOBLOCK_INVOKE(m_DataInterFaceServer, "notifyAppendThreatDataByPath", readylist.at(j));
        }
    }
}

void ThreatsOPerationThread::deleteThreatsFile(QString path)
{
    QStringList threatslist = path.split("[*]");
    int threatsSize = threatslist.size() - 1;
    for (int i = 0; i < threatsSize; i++) {
        // 先查询这个操作了的异常是否在表中
        QDBusMessage msg = DBUS_BLOCK_INVOKE(m_DataInterFaceServer, "notifySelectThreatDataByPath", threatslist.at(i));
        GET_MESSAGE_VALUE(bool, bFlag, msg);
        if (bFlag) {
            // 如果都有就移除
            QDBusMessage msg = DBUS_BLOCK_INVOKE(m_DataInterFaceServer, "notifyDeleteThreatDataByPath", threatslist.at(i));
            GET_MESSAGE_VALUE(bool, bFlag1, msg);
            if (!bFlag1) {
                qDebug() << "删除失败=======" << threatslist.at(i);
            }
        }
    }
}

void ThreatsOPerationThread::batchOPeration(QStringList list, int nType)
{
    m_list = list;
    m_type = Itemtype(nType);
    QTimer *m_t1 = new QTimer(this);
    connect(m_t1, SIGNAL(timeout()), this, SLOT(timeOperationThreats()));
    m_t1->start(30);
}

void ThreatsOPerationThread::timeOperationThreats()
{
    if (m_list.size() != 0) {
        switch (m_type) {
        case Itemtype::Repair:
            //修复
            Q_EMIT onAllRepairProblemFile(m_list.at(0));
            break;
        case Itemtype::Delete:
            break;
        case Itemtype::isolation:
            //隔离
            Q_EMIT onAllIsolationFile(true, m_list.at(0));
            break;
        case Itemtype::Trust:
            //信任
            Q_EMIT onAllTruestFile(true, m_list.at(0));
            break;
        }
    }
    m_list.removeAt(0);
}
